//    Copyright (C) Kherty.  All rights reserved.
using System.Windows;
using System.Windows.Controls;

namespace OpenLS.Spreadsheet.UI.Internals
{
    class GridExtensions
    {


        public static bool GetIsCollapsed(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsCollapsedProperty);
        }

        public static void SetIsCollapsed(DependencyObject obj, bool value)
        {
            obj.SetValue(IsCollapsedProperty, value);
        }

        // Using a DependencyProperty as the backing store for IsCollapsed.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsCollapsedProperty =
            DependencyProperty.RegisterAttached("IsCollapsed", typeof(bool), typeof(GridExtensions), new UIPropertyMetadata(false, sOnCollapsedChanged));

       static  bool hasPreviousValue (DefinitionBase definitionBase, object property)
        {
           var enumerator = definitionBase.GetLocalValueEnumerator();
           while (enumerator.MoveNext())
           {
               if (enumerator.Current.Property == property)
                   return true;
           }
           return false;
        }
        private static void sOnCollapsedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is RowDefinition)
            {
                var rowDefinition = (RowDefinition)d;
                var b = (bool)e.NewValue;
                if (b)
                {
                    SetPreviousSize(rowDefinition, rowDefinition.Height);
                    SetPreviousMinimumSize(rowDefinition, rowDefinition.MinHeight);
                    rowDefinition.Height = new GridLength(0);
                    rowDefinition.MinHeight = 0;
                }
                else
                {
                    if (hasPreviousValue(rowDefinition, PreviousSizeProperty))
                        rowDefinition.Height = GetPreviousSize(rowDefinition);
                    if (hasPreviousValue(rowDefinition, PreviousMinimumSizeProperty))
                        rowDefinition.MinHeight = GetPreviousMinimumSize(rowDefinition);
                }
            }
            else
            {
                var columnDefinition = (ColumnDefinition)d;
                var b = (bool)e.NewValue;
                if (b)
                {
                    SetPreviousSize(columnDefinition, columnDefinition.Width);
                    SetPreviousMinimumSize(columnDefinition, columnDefinition.MinWidth);
                    columnDefinition.Width = new GridLength(0);
                    columnDefinition.MinWidth = 0;
                }
                else
                {
                    if (hasPreviousValue(columnDefinition, PreviousSizeProperty))
                        columnDefinition.Width =  GetPreviousSize(columnDefinition);
                    if (hasPreviousValue(columnDefinition, PreviousMinimumSizeProperty))
                        columnDefinition.MinWidth = GetPreviousMinimumSize(columnDefinition);
                }
            }
        }


        public static GridLength GetPreviousSize(DependencyObject obj)
        {
            return (GridLength)obj.GetValue(PreviousSizeProperty);
        }

        public static void SetPreviousSize(DependencyObject obj, GridLength value)
        {
            obj.SetValue(PreviousSizeProperty, value);
        }

        // Using a DependencyProperty as the backing store for PreviousSize.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PreviousSizeProperty =
            DependencyProperty.RegisterAttached("PreviousSize", typeof(GridLength), typeof(GridExtensions), new UIPropertyMetadata(new GridLength(1, GridUnitType.Star)));



        public static double GetPreviousMinimumSize(DependencyObject obj)
        {
            return (double)obj.GetValue(PreviousMinimumSizeProperty);
        }

        public static void SetPreviousMinimumSize(DependencyObject obj, double value)
        {
            obj.SetValue(PreviousMinimumSizeProperty, value);
        }

        // Using a DependencyProperty as the backing store for PreviousMinimumSize.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PreviousMinimumSizeProperty =
            DependencyProperty.RegisterAttached("PreviousMinimumSize", typeof(double), typeof(GridExtensions), new UIPropertyMetadata( (0d)));



    }
}